<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>医疗报销系统</h2>
      </div>
      <div class="login-form">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="0px">
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              placeholder="用户名" 
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              type="password" 
              placeholder="密码" 
              @keyup.enter="handleLogin"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const loginFormRef = ref(null)

onMounted(() => {
  console.log('Login component mounted')
  console.log('Element Plus available:', typeof window.ElementPlus !== 'undefined')
})

const handleLogin = async () => {
  if (!loginFormRef.value) {
    console.error('Form ref not found')
    return
  }
  
  try {
    loading.value = true
    await loginFormRef.value.validate()
    
    // 模拟登录成功
    setTimeout(() => {
      localStorage.setItem('token', 'demo-token')
      loading.value = false
      router.push('/home')
    }, 1000)
    
  } catch (error) {
    loading.value = false
    console.error('表单验证失败', error)
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-weight: 500;
  color: #409EFF;
  margin: 0;
}

.login-button {
  width: 100%;
}
</style> 